//
//  HYCSecondPushdisController.m
//  FamilyTravel
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCSecondPushdisController.h"
#import "HYCHeadDocument.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define SWIDTH [UIScreen mainScreen].bounds.size.width
#define SHEIGHT [UIScreen mainScreen].bounds.size.height


@interface HYCSecondPushdisController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *scrArr;
@property(nonatomic, retain)NSMutableArray *lowArr;
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, retain)UIScrollView *scrollView;


@end

@implementation HYCSecondPushdisController
- (void)dealloc {
    [_hud release];
    [_arr release];
    [_scrArr release];
    [_tableView release];
    [_scrollView release];
    [_model release];
    
    
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = self.model.cnname;
    
    self.arr = [NSMutableArray array];
    self.lowArr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    //    self.tableView.rowHeight = 150;
    //横岗去掉
    self.tableView.separatorStyle = NO;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , (HEIGHT + 64) / 3)];
    
    self.tableView.tableHeaderView = self.scrollView;
    self.scrollView.contentSize = CGSizeMake(WIDTH * (self.scrArr.count + 2), 0);
    self.scrollView.pagingEnabled = YES;
    //    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [_scrollView release];
    
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeAction) userInfo:nil repeats:YES];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    [self createData];
    
}


//返回tableView个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        
        return self.arr.count;
    }else {
        
        return self.lowArr.count;
    }
    
}

- (void)createData {
    
    NSString *strURL = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@", self.model.number];
    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", strURL, @"&page=1&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&v=1"];
    
    
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        for (NSDictionary *tempDic in dic[@"data"][@"hot_city"]) {
            HYCSecondPushDisModel *movie = [[HYCSecondPushDisModel alloc] init];
            [movie setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:movie];
            [movie release];
            
        }
        
        for (NSDictionary *tempDic in dic[@"data"][@"new_discount"]) {
            HYCSecPushSecDisModel *movie = [[HYCSecPushSecDisModel alloc] init];
            [movie setValuesForKeysWithDictionary:tempDic];
            [self.lowArr addObject:movie];
            [movie release];
            
        }
        
        // 轮播图
        self.scrArr = [NSMutableArray array];
        for (NSString *tempStr in dic[@"data"][@"photos"]) {
            HYCScrSecPushsecDisModel  *scrMovie = [[HYCScrSecPushsecDisModel alloc] init];
            scrMovie.photos = tempStr;
            [self.scrArr addObject:scrMovie];
            [scrMovie release];
        }
        UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (HEIGHT + 64) / 3)];
        [firstImageView sd_setImageWithURL:[NSURL URLWithString:[self.scrArr[self.scrArr.count - 1] photos]]];
        [self.scrollView addSubview:firstImageView];
        [firstImageView release];
        
        for (NSInteger i = 0; i < self.scrArr.count; i++) {
            UIImageView *scrImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, (HEIGHT + 64) / 3)];
            [scrImageView sd_setImageWithURL:[NSURL URLWithString:[self.scrArr[i] photos]]];
            [self.scrollView addSubview:scrImageView];
            [scrImageView release];
        }
        UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * self.scrArr.count, 0, WIDTH, (HEIGHT + 64) / 3)];
        [lastImageView sd_setImageWithURL:[NSURL URLWithString:[self.scrArr[0] photos]]];
        [self.scrollView addSubview:lastImageView];
        [lastImageView release];
        lastImageView.userInteractionEnabled = YES;
        self.scrollView.contentSize = CGSizeMake(WIDTH * ( self.scrArr.count + 2), 0);
        
        [self.hud removeFromSuperview];
        [self.tableView reloadData];
        
        
    }];
    
}

// 轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollView.contentOffset.x == (self.scrArr.count + 1) * WIDTH) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }else if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.scrArr.count * WIDTH, 0);
    }
    
}
- (void)changeAction{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0) animated:YES];
    // 判断是否到当前的最后一张, 如果到了, 需要把偏移量设置到第一张图片的位置
    if (self.scrollView.contentOffset.x == self.scrArr.count * WIDTH) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
}


// 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.lowArr.count == 0) {
        return 180;
    }else {
        if (indexPath.row <= self.arr.count) {
            return 180;
        }else {
            return 150;
        }
    }
}

// 当前分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.lowArr.count == 0) {
        return 1;
    }else{
        return 2;
    }
    
}
// 给分区设定标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (self.lowArr.count == 0) {
        return @"热门城市";
    }else{
        if (section == 0) {
            return @"热门城市";
        }
        return @"其他城市";
    }
    
    
    
}

// 返回分区标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

// 自定义分区标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (self.lowArr.count == 0) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, WIDTH - 10, 25)];
        //        footerView.backgroundColor = [UIColor redColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        //                button.backgroundColor = [UIColor redColor];
        button.frame = CGRectMake(WIDTH / 1.45, -6, 100, 15);
        button.font = [UIFont systemFontOfSize:15];
        [button setTitle:@"查看全部" forState:UIControlStateNormal];
        [footerView addSubview:button];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        //        button.backgroundColor = [UIColor redColor];
        [button release];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, -6, 100, 15)];
        label.text = @"热门城市";
        [footerView addSubview:label];
        label.font = [UIFont systemFontOfSize:15];
        //        label.backgroundColor = [UIColor redColor];
        
        [label release];
        
        return footerView;
    }else {
        
        
        
        if (section == 0) {
            UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, WIDTH - 10, 25)];
            //        footerView.backgroundColor = [UIColor redColor];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            //                button.backgroundColor = [UIColor redColor];
            button.frame = CGRectMake(WIDTH / 1.45, -6, 100, 15);
            button.font = [UIFont systemFontOfSize:15];
            [button setTitle:@"查看全部" forState:UIControlStateNormal];
            [footerView addSubview:button];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            //        button.backgroundColor = [UIColor redColor];
            [button release];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, -6, 100, 15)];
            label.text = @"热门城市";
            [footerView addSubview:label];
            label.font = [UIFont systemFontOfSize:15];
            //        label.backgroundColor = [UIColor redColor];
            
            [label release];
            
            return footerView;
        }else {
            UIView *View = [[UIView alloc] initWithFrame:CGRectMake(5, 0, WIDTH - 10, 1)];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(WIDTH / 1.45, -6, 100, 15);
            [button setTitle:@"查看全部" forState:UIControlStateNormal];
            button.font = [UIFont systemFontOfSize:15];
            [View addSubview:button];
            
            [button addTarget:self action:@selector(secBbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, -6, 100, 15)];
            label.text = @"超值自由行";
            label.font = [UIFont systemFontOfSize:15];
            [View addSubview:label];
            [label release];
            
            return View;
        }
    }
}

- (void)buttonAction:(UIButton *)button {
    HYCPushAllCitytroller *pDVC = [[HYCPushAllCitytroller alloc] init];
    [self.navigationController pushViewController:pDVC animated:YES];
    //
    pDVC.title = @"全部城市";
    pDVC.str = self.model.number;
    pDVC.hidesBottomBarWhenPushed = YES;
    
}

- (void)secBbuttonAction:(UIButton *)button {
    HYCPushAllFreetroller *pAFVC = [[HYCPushAllFreetroller alloc] init];
    
    [self.navigationController pushViewController:pAFVC animated:YES];
    pAFVC.title = @"精彩当地游";
    
    pAFVC.str = self.model.number;
    pAFVC.hidesBottomBarWhenPushed = YES;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HYCPushCitytroller *pCVC = [[HYCPushCitytroller alloc] init];
    [self.navigationController pushViewController:pCVC animated:YES];
    pCVC.model = self.arr[indexPath.row];
    //    pCVC.hidesBottomBarWhenPushed = YES;
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        HYCSecondPushDisCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[HYCSecondPushDisCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        }
        
        HYCSecondPushDisModel *model = _arr[indexPath.row];
        cell.model = model;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
        return cell;
        
    } else {
        static NSString *reuse= @"reuseSencond";
        HYCSecPushSecDisCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[HYCSecPushSecDisCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        }
        HYCSecPushSecDisModel *model = _lowArr[indexPath.row];
        cell.model = model;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
        
        return cell;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
